package org.han.permission.controller;

import org.han.annotation.RonghuanetPermission;
import org.han.permission.domain.Menu;
import org.han.permission.query.MenuQuery;
import org.han.permission.service.IMenuService;
import org.han.util.AjaxResult;
import org.han.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/menu")
@RonghuanetPermission(name = "菜单管理")
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class MenuConntroller {

    @Autowired
    private IMenuService menuService;

    @RonghuanetPermission(name = "查询菜单树")
    @GetMapping("/tree")
    public AjaxResult tree(Long userId){
        try {
            List<Menu> tree = menuService.tree(userId);
            return AjaxResult.me().setData(tree);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询失败" + e.getMessage());
        }
    }
    @RonghuanetPermission(name = "分页查询")
    @PostMapping
    public AjaxResult listPage(@RequestBody MenuQuery query){
        try {
            PageList pageList = menuService.pageList(query);
            return AjaxResult.me().setData(pageList);

        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询失败~~" + e.getMessage());
        }
    }
    @PutMapping
    @RonghuanetPermission(name = "菜单的新增/修改")
    public AjaxResult addorudpate(@RequestBody Menu menu){
        try {
            if(Objects.isNull(menu.getId())){
                //新增
                menuService.insert(menu);
            }else {
                //修改
                menuService.update(menu);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("新增/修改失败" + e.getMessage());
        }
    }
    @RonghuanetPermission(name = "菜单的删除")
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id")Long id){
        try {
            menuService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败" + e.getMessage());
        }
    }
    @GetMapping("/{id}")
    @RonghuanetPermission(name = "菜单的根据id查询")
    public AjaxResult selectById(@PathVariable("id")Long id){
        try {
            Menu role = menuService.selectById(id);
            return AjaxResult.me().setData(role);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询失败" + e.getMessage());
        }
    }

    @GetMapping
    @RonghuanetPermission(name = "菜单的查询所有")
    public AjaxResult selectAll(){
        try {
            List<Menu> roles = menuService.selectAll();
            return AjaxResult.me().setData(roles);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询失败" + e.getMessage());
        }
    }
    @PatchMapping
    @RonghuanetPermission(name = "菜单的批量删除")
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        try {
            menuService.batchDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询失败" + e.getMessage());
        }
    }
}
